package com.wxapp.dispatch.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.dispatch.api.pojo.entity.DispatchDO;
import com.wxapp.dispatch.api.pojo.entity.DispatchLogsDO;
import com.wxapp.dispatch.api.pojo.vm.dispatchlogs.PDispatchLogDetailVm;
import com.wxapp.dispatch.api.pojo.vm.dispatchlogs.PDispatchLogPageVm;
import com.wxapp.dispatch.api.pojo.vo.dispatchlogs.QDispatchLogPageVo;
import com.wxapp.dispatch.service.mapper.DispatchLogsMapper;
import com.wxapp.dispatch.service.mapper.DispatchMapper;
import com.wxapp.dispatch.service.service.DispatchLogsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/5/26 18:13
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class DispatchLogsServiceImpl extends IBaseService implements DispatchLogsService {
    private final DispatchLogsMapper dispatchLogsMapper;
    private final DispatchMapper dispatchMapper;

    @Override
    public ResultVm<Page<PDispatchLogPageVm>> page(QDispatchLogPageVo vm) {
        var condition = ConditionUtil.getSCondition(DispatchLogsDO::new, vm, PDispatchLogPageVm.class, null,"", null);
        condition.le(CheckUtil.isNotNullObject(vm.getEndTime()),DispatchLogsDO::getCreateTime,vm.getEndTime());
        condition.lt(CheckUtil.isNotNullObject(vm.getStartTime()),DispatchLogsDO::getCreateTime,vm.getStartTime());
        var pages = ConditionUtil.getPage(vm, DispatchLogsDO.class);
        var logsDOPage = dispatchLogsMapper.selectPage(pages, condition);
        Page<PDispatchLogPageVm> result = new Page<>();
        if (!logsDOPage.getRecords().isEmpty()){
            var dispatchCodes = logsDOPage.getRecords().stream().map(DispatchLogsDO::getDispatchCode).distinct().toList();
            var queryWrapper = new LambdaQueryWrapper<DispatchDO>();
            queryWrapper.like(CheckUtil.isNotNullString(vm.getName()),DispatchDO::getName, vm.getName());
            queryWrapper.in(!dispatchCodes.isEmpty(),DispatchDO::getCode,dispatchCodes);
            var dispatchDOS = dispatchMapper.selectList(queryWrapper);
            if (CheckUtil.isNotNullList(dispatchDOS)) {
                result=CopyUtil.copyPage(logsDOPage, PDispatchLogPageVm::new, (s, r)->{
                    dispatchDOS.stream().filter(st->st.getCode().equals(s.getDispatchCode())).findFirst().ifPresent(st->{
                        r.setName(st.getName());
                    });
                    return null;
                });
            }
        }
        return success(result);
    }

    @Override
    public ResultVm<PDispatchLogDetailVm> detail(String id) {
        var dispatchLog = dispatchLogsMapper.selectById(id);
        if (CheckUtil.isNullObject(dispatchLog)) {
            return error("没有找到日志信息");
        }
        var queryWrapper = new LambdaQueryWrapper<DispatchDO>();
        queryWrapper.in(DispatchDO::getCode,dispatchLog.getDispatchCode());
        var dispatchDOS = dispatchMapper.selectFirst(queryWrapper);
        if (CheckUtil.isNullObject(dispatchDOS)) {
            return error("没有找到任务信息");
        }
        return success(CopyUtil.copyObjectNew(dispatchLog, PDispatchLogDetailVm::new, (s, r)->{
            r.setName(dispatchDOS.getName());
            return null;
        }));
    }

    @Override
    public ResultVm<PDispatchLogDetailVm> delete(LocalDateTime endTime) {
        var wrapper = new LambdaUpdateWrapper<DispatchLogsDO>();
        wrapper.le(DispatchLogsDO::getCreateTime, endTime);
        dispatchLogsMapper.delete(wrapper);
        return success("删除成功");
    }
}
